<template>
	<div class="system-dic-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px" @close="clean">
			<el-form ref="credentialDialogFormRef" :rules="rules" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="名称" prop="name">
							<el-input v-model="state.ruleForm.name" placeholder="请输入名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="IP" prop="meta.ip">
							<el-input v-model="state.ruleForm.meta.ip" placeholder="请输入IP" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="端口" prop="meta.port">
							<el-input type="number" v-model="state.ruleForm.meta.port" placeholder="请输入端口" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="密码" prop="meta.credential">
							<el-select v-model="state.ruleForm.meta.credential" class="filter-item" placeholder="请选择">
								<el-option v-for="item in state.credentialOptions" :key="item.value" :label="item.label" :value="item.value" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="描述" prop="description">
							<el-input v-model="state.ruleForm.description" type="textarea" placeholder="请输入描述" maxlength="200"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemDicDialog">
import { reactive, ref } from 'vue';
import { resourceApi } from '/@/api/resource';
import Constants from '/@/constant/constants';
import { ElMessage } from 'element-plus';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

const rules = reactive({
	name: [{ required: true, message: '名称必填', trigger: 'blur' }],
	'meta.credential': [{ required: true, message: '密码必填', trigger: 'blur' }],
});

// 定义变量内容
const credentialDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		id: undefined,
		type: Constants.RESOURCE_TYPE.SERVER,
		name: '', // 资源名称
		description: '', // 描述
		meta: {
			ip: '', // IP
			credential: undefined, // 配置文件
			port: 0, // 端口
		},
	},
	credentialOptions: [
		{
			label: '密码',
			value: 'password',
		},
	],
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type: string, row: Object) => {
	resourceApi()
		.getOptions({ types: [Constants.RESOURCE_TYPE.CREDENTIAL] })
		.then((res) => {
			if (res.data) {
				state.credentialOptions = res.data[Constants.RESOURCE_TYPE.CREDENTIAL];
			}
		})
		.catch((err) => {
			ElMessage.error(err.msg);
		});

	if (type === 'edit') {
		state.ruleForm = row;
		state.dialog.title = '修改配置';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增配置';
		state.dialog.submitTxt = '新 增';
	}
	state.dialog.isShowDialog = true;
};

// 清空表单
const clean = () => {
	state.ruleForm = {
		id: undefined,
		type: Constants.RESOURCE_TYPE.SERVER,
		name: '', // 资源名称
		description: '', // 描述
		meta: {
			ip: '', // IP
			credential: undefined, // 配置文件
			port: 0, // 端口
		},
	};
};

// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	credentialDialogFormRef.value.validate((valid: boolean) => {
		if (valid) {
			resourceApi()
				.addResource(state.ruleForm)
				.then(() => {
					closeDialog();
					emit('refresh');
					ElMessage.success('操作成功');
				})
				.catch((err) => {
					ElMessage.error(err.msg);
				});
		}
	});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
